/**
 * 获取URL参数
 * @returns {string|{}|null}
 */
export function getUrlParams() {
    let name = arguments.length > 0 ? arguments[0] : null;
    let index = location.href.indexOf('?')
    if (index < 0) {
        return null
    }
    let queryStr = decodeURIComponent(location.href.slice(index + 1))
    let paramsArr = queryStr.split('&')
    if (paramsArr.length === 0) {
        return null
    }
    let obj = {}
    for (let i = 0; i < paramsArr.length; i++) {
        let q = paramsArr[i].indexOf('=')
        let key = paramsArr[i].slice(0, q)
        let value = paramsArr[i].slice(q + 1)
        if (key === name) {
            return value
        } else {
            obj[key] = value
        }
    }
    return obj

}

const loginInfo = {
    // 可选
    apiVer: 1,
    // 必选
    appType: 'mr',
    // 必选
    companyCode: 'wwh_mr_pos',
    // 可选
    langCode: 'zh-cn',
    // 可选
    machineId: '631',
    // 可选
    machineModel: 'Simulator',
    // 可选
    userAccount: 'HD01',
    // 可选
    osVersion: '13.3',
    // 必选
    packageName: 'com.Regentsoft.YunPOSApp.test',
    // 可选
    platformId: 3,
    // 可选
    requestId: 1593585869954,
    // 可选
    token: 'CECAB2C72DA8FEDF8DE288704349FD4E',
    // 可选
    uuid: '98685333-F11D-4447-A4DB-5EF60B445B2C',
    // 可选
    versionCode: '40600',
    // 可选
    versionName: '4.6.0',
    // 渠道编码
    channelCode: '10021',
    // 渠道
    channelId: '10021',
    // 模块编号
    moduleId: '106',

    logisticsUrl: 'http://192.168.9.78:50099/ers/mr/'

}

/**
 * 获取用户登录信息
 */
export function getLoginInfo() {
    let urlParams = getUrlParams();
    if (!urlParams) {
        try {
            urlParams = JSON.parse(window.android.getInfo())
        } catch (e) {
            console.error('URL参数不存在，window.android.getInfo方法不存在，使用了默认数据')
            urlParams = loginInfo
        }
        window.loginInfo = urlParams
        sessionStorage.setItem('loginInfo', JSON.stringify(urlParams))
        return urlParams
    }
    // 存储登录信息
    sessionStorage.setItem('loginInfo', JSON.stringify(urlParams))
    window.loginInfo = urlParams
    if (['3', '8'].includes(urlParams.platformId)) {
        // 当IOS平台时覆盖导航栏颜色
        window.topAreaBackgroundColor = function (handlerInterface, handlerMethod, parameters) {
            let dic = {
                'handlerInterface': handlerInterface,
                'function': handlerMethod,
                'parameters': parameters
            }
            if (window.webkit && window.webkit.messageHandlers) {
                window.webkit.messageHandlers[handlerInterface].postMessage(dic)
            }
        }
        window.topAreaBackgroundColor(
            'native',
            'topAreaBackgroundColor',
            '#101E40'
        )
    }
    return urlParams
}

/**
 * 获取语言
 */
export function getLang(lang) {
    if (!lang) {
        return 'zh-cn'
    }
    let two = lang.toLowerCase().substr(0, 2)
    let hasZh = two === 'zh'
    if (hasZh) {
        let isTw = lang.toLowerCase().substr(3, 2) === 'tw'
        let isHant = lang.toLowerCase().substr(3, 4) === 'hant'
        let isHK = lang.toLowerCase().includes('hk')
        let isMO = lang.toLowerCase().includes('mo')
        if (isTw || isHant || isHK || isMO) {
            lang = 'zh-hant'
        } else {
            lang = 'zh-cn'
        }
    } else if (two === 'en') {
        lang = 'en'
    } else {
        lang = 'en'
    }
    return lang
}

let loginInfoData = getLoginInfo() || loginInfo;
let lang = getLang(loginInfoData.langCode);

sessionStorage.setItem('loginInfo', JSON.stringify(loginInfoData));
sessionStorage.setItem('lang', lang);

if (loginInfoData.models.toLocaleLowerCase() === 'pad') {
    window.location.href = './mobile.html'
} else {
    window.location.href = './tablet.html'
}
